package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.CheckMenuDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.CheckMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class CheckMenuServiceImpl implements CheckMenuService {
    @Autowired
    private CheckMenuDao checkMenuDao;

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        log.info("[检查组-分页查询]data：{}", queryPageBean);
        //设置分页参数
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //查询page
        Page<Menu> page = checkMenuDao.selectByCondition(queryPageBean.getQueryString());
        //封装返回值
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public List<Role> findAll() {
        return checkMenuDao.selectAll();
    }

    @Transactional
    @Override
    public void add(Menu menu, Integer[] roleIds) {
        log.info("LOGPJSDNAa{}", menu);
        log.info("LOGPJSDNAa{}", Arrays.asList(roleIds));
        // 插入基本数据
        checkMenuDao.insert(menu);
        log.info("LOGGOLGOASBJKHASB DONE+++{}", roleIds);
        //逐条插入关系表数据
        for (Integer roleId : roleIds) {
            log.info("LOGSUAJDB FIRST{},ANOTHER{}", roleId, menu.getId());
            checkMenuDao.insertMenuAndRole(roleId, menu.getId());
        }
    }

    @Transactional
    @Override
    public void delete(Integer menuId) {
        int i = checkMenuDao.checkIfCanDelete(menuId);
        if (i != 0) {
            throw new BusinessRuntimeException("有关联表，信不信我捶你？");
        } else {
            log.info("LOGPNFUJSNAa{}", menuId);
            checkMenuDao.deleteRoleAndMenu(menuId);
            log.info("first done{}", menuId);
            checkMenuDao.deleteByMenuId(menuId);
            log.info("second done{}", menuId);
        }
    }

    @Override
    public Menu findById(Integer menuId) {
        return checkMenuDao.selectById(menuId);
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        return checkMenuDao.selectCheckItemIdsByCheckGroupId(id);
    }

    @Transactional
    @Override
    public void edit(Menu menu, Integer[] roleIds) {
        //1、更新menu
        checkMenuDao.update(menu);
        log.info("UPDATE DONE ASNDASJD");
        //2、更新关联关系
        //2.1、删除原有关系
        checkMenuDao.deleteMenuAndRoleByMenuId(menu.getId());
        log.info("UPDATE DONE BBBBB");
//        2.2、加入当前关联关系
        for (Integer roleId : roleIds) {
            checkMenuDao.insertRoleIdAndRoleId(menu.getId(), roleId);
        }
        log.info("UPDATE DONE CCCCC");
    }
}
